#coding:utf-8
from django.template import RequestContext
from django.http import HttpResponse,HttpResponseRedirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render
import json

import common.httpclient as http
import common.tips as tips
import common.modelAdminUser as modelAdminUser
import config.myurls as myurls
import common.function as myfc
import common.category as mycategory

import math

def prizedel(req):
  '删除标的奖励配置'
  
  rt={}
  rt['response_code']=0
  rt['response_message']="待提交"
  
  id=req.GET.get("id",0)
  if id == 0:
    rt['response_code']=-1
    rt['response_message']="id不能为0"
    return tips.returnAjax(rt)
  
  param=[]
  param.append(("id",id))
  
  url=myurls.ApiUpdBorrowprizedel;
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
  except HTTPError:
    pass
  return tips.returnAjax(rt)

def all(req):
  '标的列表'
  
  main_nav=[]
  
  context={}
  context['title'] = '-标的列表'
  
  context['GET']=req.GET
  context['arr_borrow_status']=mycategory.getCategoryByName("borrow_status",req)
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetBorrowList;
  
  param=[];
  rt=[]
  
  param.append(("p",req.GET.get("page",1)))
  param.append(("pageSize",req.GET.get("pageSize",10)))
  
  id=req.GET.get("id",0)
  if id != 0:
    param.append(("id",id))
  borrow_name=req.GET.get("borrow_name","")
  if borrow_name != "":
    param.append(("borrow_name",borrow_name))
  borrow_status=req.GET.get("borrow_status",0)
  if borrow_status != 0:
    param.append(("borrow_status",borrow_status))
  
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  
  main_nav.append({"url":"","name":"标的列表("+str(rt['count'])+")"})
  main_nav.append({"url":"javascript:showSearchDiv();","name":"搜索"})
  main_nav.append({"url":"/borrow/add/","name":"添加"})
  context['main_nav']=main_nav
  
  context['list']=rt['list']
  nav_list=[0]*math.ceil(int(rt['count'])/int(rt['pageSize']))
  paginator = Paginator(nav_list,1)
  current_page_num = req.GET.get('page',1)
  try:
    current_page = paginator.page(current_page_num)
  except PageNotAnInteger:
    current_page = paginator.page(1)
  except EmptyPage:
    current_page = paginator.page(paginator.num_pages)
  context['current_page']=current_page
  context['paginator']=paginator
  more_param=""
  for key in req.GET:
    if key != "page":
      more_param+="&"+key+"="+req.GET.get(key)
  context['more_param']=more_param
  
  return render(req, 'borrow_list.html', context)

def edit(req):
  '标的编辑'
  
  
  id=0
  try:
    id=int(str(req.GET.get("id")))
  except ValueError:
    pass
  
  if id==0:
    return tips.error(req,"id不能为0","")
  
  main_nav=[]
  main_nav.append({"url":"/borrow/all/","name":"标的列表"})
  main_nav.append({"url":"","name":"标的编辑"})
  
  context={}
  context['title'] = '-标的编辑'
  context['main_nav']=main_nav
  
  
  url=myurls.ApiGetBorrowInfo;
  
  param=[];
  param.append(("id",id))
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  context['info']=json.loads(rt['info'])
  return render(req, 'borrow_edit.html', context)

def add(req):
  '添加标的'
  
  if req.method != "POST":
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"","name":"添加标的"})
    
    context={}
    context['title'] = '-添加标的'
    context['main_nav']=main_nav
    
    id=0
    try:
      id=int(str(req.GET.get("id")))
    except ValueError:
      pass
    if id > 0:
      param=[];
      param.append(("id",id))
      url=myurls.ApiGetBorrowInfo;
      rt={}
      try:
        rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
        if rt['response_code'] != 1:
          return tips.error(req,rt['response_message'])
      except HTTPError:
        pass
      
      similar_info=[]
      info=json.loads(rt['info'])
      context['info']=info
      for key in info:
        if key not in ['id','borrow_name','borrow_name_long','proid','borrow_status','add_time','collect_time','full_time','recheck_time','prize_value','investment_total_money','recommend_num','near_repayment_time','update_time','apply_repayment_time','apply_repayment_reason','end_repayment_time','repayment_end_dt','total','recommend_buy_money','creditassign_fee']:
          new_obj={}
          new_obj['k']=key
          new_obj['v']=info[key]
          similar_info.append(new_obj)
      
      context['similar_info']=similar_info
      #return HttpResponse(str(context))
    
    return render(req, 'borrow_add.html', context)
  else:
    url=myurls.ApiAddBorrow;
  
    param=[];
    for key in req.POST:
      param.append((key,req.POST.get(key)))
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")
    

def basic(req):
  '基本信息'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"基本信息"})
    
    context={}
    context['title'] = '-基本信息'
    context['main_nav']=main_nav
    context['borrow_info_tab']=mycategory.getCategoryByName("borrow_info_tab",req)
    context['whether_or_not']=mycategory.getCategoryByName("whether_or_not",req)
    
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['info']=json.loads(rt['info'])
    
    #return HttpResponse(str(context))
    
    return render(req, 'borrow_basic.html', context)
  else:
    url=myurls.ApiUpdBorrowInfoBasic;
  
    param=[];
    for key in req.POST:
      param.append((key,req.POST.get(key)))
    
    tab_str="0"
    arr_borrow_info_tab=req.POST.getlist("cbl_borrow_info_tab")
    for v in arr_borrow_info_tab:
      tab_str+=","+v
    tab_str+=",0"
    if tab_str != "0,0":
      param.append(("tab_str",tab_str))
    
    
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")

def need(req):
  '必填信息'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"必填信息"})
    
    context={}
    context['title'] = '-必填信息'
    context['main_nav']=main_nav
    context['whether_or_not']=mycategory.getCategoryByName("whether_or_not",req)
    context['arr_repayment_type']=mycategory.getCategoryByName("repayment_type",req)
    context['arr_borrow_status']=mycategory.getCategoryByName("borrow_status",req)
    #获取资产列表
    url=myurls.ApiGetAssetsList
    param=[];
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['assets_list']=rt['list']
    
    #return HttpResponse(str(context))
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    #return HttpResponse(str(context))
    
    return render(req, 'borrow_need.html', context)
  else:
    url=myurls.ApiUpdBorrowInfoNeed;
  
    param=[];
    for key in req.POST:
      param.append((key,req.POST.get(key)))
    
    #return HttpResponse(str(param))
    
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")

def prizecoupons(req):
  '现金抵扣券'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"现金抵扣券"})
    
    context={}
    context['title'] = '-现金抵扣券'
    context['main_nav']=main_nav
    context['borrow_prize_calculation_type']=mycategory.getCategoryByName("borrow_prize_calculation_type",req)
    context['available_end_type_arr']=mycategory.getCategoryByName("available_end_type_arr",req)
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    param=[];
    param.append(("borrow_id",id))
    param.append(("pageSize",50))
    url=myurls.ApiGetBorrowprizecouponsList;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['list']=rt['list']
    
    return render(req, 'borrow_prizecoupons.html', context)
  else:
    url=myurls.ApiUpdBorrowprizecoupons;
    
    
    param=[];
    for key in req.POST:
      if len(req.POST.getlist(key,[])) == 1:
        param.append((key,req.POST.get(key)))
    
    list_str=[]
    length=len(req.POST.getlist("id",[]))
    
    for i in range(length):
      new_obj={}
      rid=abs(int(req.POST.getlist("id",[])[i]))
      for key in req.POST:
        if len(req.POST.getlist(key,[])) == length:
          v=req.POST.getlist(key,[])[i]
          if key.find("ttstamp~")==-1:
            new_obj[key]=v
          else:
            if v !="":
              new_obj[key.replace("ttstamp~","")]=myfc.strToTimestamp(v)
            else:
              new_obj[key.replace("ttstamp~","")]=0
      new_obj['calculation_type']=req.POST.get("calculation_type"+str(rid))
      new_obj['available_end_type']=req.POST.get("available_end_type"+str(rid))
      new_obj['borrow_id']=req.POST.get("borrow_id")
      list_str.append(new_obj)
    
    param.append(("list_str",json.dumps(list_str)))
    
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")

def prizebonus(req):
  '红包设置'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"红包设置"})
    
    context={}
    context['title'] = '-红包设置'
    context['main_nav']=main_nav
    context['borrow_prize_calculation_type']=mycategory.getCategoryByName("borrow_prize_calculation_type",req)
    context['available_end_type_arr']=mycategory.getCategoryByName("available_end_type_arr",req)
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    param=[];
    param.append(("borrow_id",id))
    param.append(("pageSize",50))
    url=myurls.ApiGetBorrowprizebonusList;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['list']=rt['list']
    
    return render(req, 'borrow_prizebonus.html', context)
  else:
    url=myurls.ApiUpdBorrowprizebonus;
    
    
    param=[];
    for key in req.POST:
      if len(req.POST.getlist(key,[])) == 1:
        param.append((key,req.POST.get(key)))
    
    list_str=[]
    length=len(req.POST.getlist("id",[]))
    
    for i in range(length):
      new_obj={}
      rid=abs(int(req.POST.getlist("id",[])[i]))
      for key in req.POST:
        if len(req.POST.getlist(key,[])) == length:
          v=req.POST.getlist(key,[])[i]
          if key.find("ttstamp~")==-1:
            new_obj[key]=v
          else:
            if v !="":
              new_obj[key.replace("ttstamp~","")]=myfc.strToTimestamp(v)
            else:
              new_obj[key.replace("ttstamp~","")]=0
      new_obj['calculation_type']=req.POST.get("calculation_type"+str(rid))
      new_obj['available_end_type']=req.POST.get("available_end_type"+str(rid))
      new_obj['borrow_id']=req.POST.get("borrow_id")
      list_str.append(new_obj)
    
    param.append(("list_str",json.dumps(list_str)))
    
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")
    
def prizeticket(req):
  '加息券'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"加息券"})
    
    context={}
    context['title'] = '-加息券'
    context['main_nav']=main_nav
    context['borrow_prize_calculation_type']=mycategory.getCategoryByName("borrow_prize_calculation_type",req)
    context['available_end_type_arr']=mycategory.getCategoryByName("available_end_type_arr",req)
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    param=[];
    param.append(("borrow_id",id))
    param.append(("pageSize",50))
    url=myurls.ApiGetBorrowprizeticketList;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['list']=rt['list']
    
    return render(req, 'borrow_prizeticket.html', context)
  else:
    url=myurls.ApiUpdBorrowprizeticket;
    
    
    param=[];
    for key in req.POST:
      if len(req.POST.getlist(key,[])) == 1:
        param.append((key,req.POST.get(key)))
    
    list_str=[]
    length=len(req.POST.getlist("id",[]))
    
    for i in range(length):
      new_obj={}
      rid=abs(int(req.POST.getlist("id",[])[i]))
      for key in req.POST:
        if len(req.POST.getlist(key,[])) == length:
          v=req.POST.getlist(key,[])[i]
          if key.find("ttstamp~")==-1:
            new_obj[key]=v
          else:
            if v !="":
              new_obj[key.replace("ttstamp~","")]=myfc.strToTimestamp(v)
            else:
              new_obj[key.replace("ttstamp~","")]=0
      new_obj['calculation_type']=req.POST.get("calculation_type"+str(rid))
      new_obj['available_end_type']=req.POST.get("available_end_type"+str(rid))
      new_obj['borrow_id']=req.POST.get("borrow_id")
      list_str.append(new_obj)
    
    param.append(("list_str",json.dumps(list_str)))
    
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")

def prizelastinvest(req):
  '满标券'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"满标券"})
    
    context={}
    context['title'] = '-满标券'
    context['main_nav']=main_nav
    context['borrow_prize_calculation_type']=mycategory.getCategoryByName("borrow_prize_calculation_type",req)
    context['available_end_type_arr']=mycategory.getCategoryByName("available_end_type_arr",req)
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    param=[];
    param.append(("borrow_id",id))
    param.append(("pageSize",50))
    url=myurls.ApiGetBorrowprizelastinvestList;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['list']=rt['list']
    
    return render(req, 'borrow_prizelastinvest.html', context)
  else:
    url=myurls.ApiUpdBorrowprizelastinvest;
    
    
    param=[];
    list_str=[]
    new_obj={}
    for key in req.POST:
      v=req.POST.get(key)
      if key.find("ttstamp~")==-1:
        new_obj[key]=v
      else:
        if v !="":
          new_obj[key.replace("ttstamp~","")]=myfc.strToTimestamp(v)
        else:
          new_obj[key.replace("ttstamp~","")]=0
   
    list_str.append(new_obj)
    
    param.append(("borrow_id",req.POST.get("borrow_id")))
    param.append(("list_str",json.dumps(list_str)))
    
    #return HttpResponse(str(param))
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")


def pwd(req):
  '设置密码'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"设置密码"})
    
    context={}
    context['title'] = '-设置密码'
    context['main_nav']=main_nav
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    return render(req, 'borrow_pwd.html', context)
  else:
    url=myurls.ApiUpdBorrowInfoPwd;
  
    param=[];
    for key in req.POST:
      param.append((key,req.POST.get(key)))
    
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")
    
def withdraw(req):
  '提现卡设置'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"提现卡设置"})
    
    context={}
    context['title'] = '-提现卡设置'
    context['main_nav']=main_nav
    context['borrow_info_service_amount_type']=mycategory.getCategoryByName("borrow_info_service_amount_type",req)
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    
    url=myurls.ApiGetUserbanksList;
    param=[];
    param.append(("user_id",json.loads(rt['info'])['borrow_uid']))
    param.append(("allow_withdrawals",1))
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['bank_list']=rt['list']
    
    #return HttpResponse(str(rt))
    return render(req, 'borrow_withdraw.html', context)
  else:
    url=myurls.ApiUpdBorrowInfoWithdraw;
  
    param=[];
    for key in req.POST:
      param.append((key,req.POST.get(key)))
    
    rt={}
    rt['response_code']=0
    rt['response_message']="待提交"
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    except HTTPError:
      pass
    return HttpResponse(json.dumps(rt),content_type="application/json")
    
    
def invest(req):
  '募集详细'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"募集详细"})
    
    context={}
    context['title'] = '-募集详细'
    context['main_nav']=main_nav
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    param=[];
    param.append(("borrow_id",id))
    param.append(("p",req.GET.get("page",1)))
    param.append(("pageSize",req.GET.get("pageSize",10)))
    url=myurls.ApiGetBorrowInvestList;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['list']=rt['list']
    nav_list=[0]*math.ceil(int(rt['count'])/int(rt['pageSize']))
    paginator = Paginator(nav_list,1)
    current_page_num = req.GET.get('page',1)
    try:
      current_page = paginator.page(current_page_num)
    except PageNotAnInteger:
      current_page = paginator.page(1)
    except EmptyPage:
      current_page = paginator.page(paginator.num_pages)
    context['current_page']=current_page
    context['paginator']=paginator
    
    #return HttpResponse(rt['list'])
    
    return render(req, 'borrow_invest.html', context)
  else:
    pass

def repay(req):
  '还款计划'
  
  if req.method != "POST":
    id=0
    try:
      id=int(str(req.GET.get("borrow_id")))
    except ValueError:
      pass
    if id==0:
      return tips.error(req,"borrow_id不能为0","")
    
    
    
    main_nav=[]
    main_nav.append({"url":"/borrow/all/","name":"标的列表"})
    main_nav.append({"url":"/borrow/edit/?id="+str(id),"name":"标的编辑"})
    main_nav.append({"url":"","name":"还款计划"})
    
    context={}
    context['title'] = '-还款计划'
    context['main_nav']=main_nav
    
    param=[];
    param.append(("id",id))
    url=myurls.ApiGetBorrowInfo;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    
    context['info']=json.loads(rt['info'])
    
    param=[];
    param.append(("borrow_id",id))
    url=myurls.ApiGetBorrowRepayList;
    rt={}
    try:
      rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
      if rt['response_code'] != 1:
        return tips.error(req,rt['response_message'])
    except HTTPError:
      pass
    context['list']=rt['list']
    
    #return HttpResponse(str(rt['list']))
    
    return render(req, 'borrow_repay.html', context)
  else:
    pass
